package Bill.app.client;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import javax.swing.*;

import Bill.util.Configuration;

class Causerie extends Thread
{

    Causerie(JList liste, JLabel lab, String Etat)
    {
        this.Etat = null;
        this.liste = liste;
        log = lab.getToolTipText().trim();
        this.Etat = Etat;
        this.lab = lab;
        start();
    }

    public void run()
    {
        ImageIcon vert = new ImageIcon("enligne.jpg");
        ImageIcon rouge = new ImageIcon("horsligne.jpg");
        do
        {
            log = lab.getToolTipText().trim();
            Socket envoi = null;
            try
            {
                System.out.println(Etat);
                if(Etat.compareToIgnoreCase("CLOSED") == 0)
                    stop();
                String text = (new StringBuilder("2:")).append(log).toString();
                 Configuration.loadConfiguration();
                 envoi = new Socket(Configuration.IPserveur, Configuration.Config_portServeur);
                
                PrintWriter ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(envoi.getOutputStream())), true);
                BufferedReader lecture = new BufferedReader(new InputStreamReader(envoi.getInputStream()));
                ecriture.println(text);
                text = lecture.readLine();
                try
                {
                    lecture.close();
                    envoi.close();
                    ecriture.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                StringTokenizer t = new StringTokenizer(text, "=");
                liste.removeAll();
                DefaultListModel model = new DefaultListModel();
                try
                {
                    while(t.hasMoreTokens()) 
                    {
                        StringTokenizer t1 = new StringTokenizer(t.nextToken());
                        String lo = t1.nextToken();
                        String status = t1.nextToken();
                        if(lo.compareToIgnoreCase("null") != 0)
                        {
                            JLabel Label = new JLabel(lo);
                            if(status.compareToIgnoreCase("en_ligne") == 0)
                            {
                                Label.setToolTipText("votre contact est en ligne");
                                Label.setIcon(vert);
                            } else
                            {
                                Label.setToolTipText("votre contact est hors ligne");
                                Label.setIcon(rouge);
                            }
                            model.addElement(Label);
                        }
                    }
                    try
                    {
                        liste.setModel(model);
                    }
                    catch(Exception exception) { }
                }
                catch(Exception exception1) { }
            }
            catch(UnknownHostException e)
            {
                e.printStackTrace();
                try {
					sleep(60000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
            try
            {
                Thread.sleep(3000);//3s
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
        } while(true);
    }

    JList liste;
    String log;
    public String Etat;
    JLabel lab;
}
